#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define MAX 10

typedef struct Set Set;

struct Set
{
	char *v;
	int key[2];
};

void print (const Set *t)
{
  printf ("%d, the %d\n", t->key[0], t->key[1]);
}

/*
int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}



int main()
{
	int i,j =1,temp;
	struct Set t[5];
	
	
	t[0].key[0] = 4;
	t[0].key[1] = 3;
	t[1].key[0] = 4;
	t[1].key[1] = 2;
	t[2].key[0] = 4;
	t[2].key[1] = 5;
	t[3].key[0] = 3;
	t[3].key[1] = 2;
	t[4].key[0] = 3;
	t[4].key[1] = 1;

	for (i = 0; i < 5; i++)
    		print(&t[i]);
  	printf ("\n");


	qsort (t, 5, sizeof (Set), compare);
/*
	for( i =1; i< 5; i++)
	{
		if(t[i].key[0] == t[i-1].key[0])  {
			if(t[i].key[1] < t[i-1].key[1])  {
				temp = t[i].key[1] ;
				t[i].key[1] = t[i-1].key[1];
				t[i-1].key[1] = temp;
			}	
		}
	}

	for (i = 0; i < 5; i++)
    		print(&t[i]);
  	printf ("\n");


}
*/
void quicksort(int start, int last,Set **arr)
{
    int result,start1,last1;
    start1 = start;
    last1 = last;
    if(start<last)
    {
             int *str;
        int p1,p2;
        str= (*arr[last]).key; 
        p1=start-1;
        p2 = start;
        Set *temp;
        for(;p2<last;p2++)
        {
                 if(memcmp(arr[p2],str,6)<=0)
            {
                p1++;
                temp=arr[p1];
                arr[p1]=arr[p2];
                arr[p2]=temp;
            }
        }
    temp=arr[p1+1];
    arr[p1+1]=arr[last];
    arr[last]=temp;
    result = p1+1;
    quicksort(start1,result-1,arr);
    quicksort(result+1,last1,arr);
    }
}


int main()
{
	int i,j =1,temp;
	struct Set tempSet;
	struct Set t[5];
	t[0].key[0] = 4;
	t[0].key[1] = 3;
	t[1].key[0] = 4;
	t[1].key[1] = 2;
	t[2].key[0] = 2;
	t[2].key[1] = 5;
	t[3].key[0] = 3;
	t[3].key[1] = 2;
	t[4].key[0] = 3;
	t[4].key[1] = 1;

	for (i = 0; i < 5; i++)
    		print(&t[i]);
  	printf ("\n");

	quicksort(0, 4,&(t));
}
